using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Management;
using System.Net;
namespace Common.Environment
{
	public static class NetworkManager
	{
		private static Dictionary<string, NetworkInfo> _mInformations;
		private static NetworkInfo _networkInfo;
		public static NetworkInfo NetworkInfo
		{
			get
			{
				return NetworkManager._networkInfo;
			}
		}
		public static void ReadSysInfo()
		{
			NetworkManager._mInformations = new Dictionary<string, NetworkInfo>();
			ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID IS NOT NULL");
			using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectSearcher.Get().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ManagementObject managementObject = (ManagementObject)enumerator.Current;
					NetworkInfo networkInfo = new NetworkInfo
					{
						DeviceName = NetworkManager.ParseProperty(managementObject["Description"]),
						AdapterType = NetworkManager.ParseProperty(managementObject["AdapterType"]),
						MacAddress = NetworkManager.ParseProperty(managementObject["MACAddress"]),
						ConnectionID = NetworkManager.ParseProperty(managementObject["NetConnectionID"]),
						Status = (NetConnectionStatus)Convert.ToInt32(managementObject["NetConnectionStatus"])
					};
					NetworkManager.SetIp(networkInfo);
					NetworkManager._mInformations.Add(networkInfo.ConnectionID, networkInfo);
				}
			}
		}
		private static string ParseProperty(object data)
		{
			string result;
			if (data != null)
			{
				result = data.ToString();
			}
			else
			{
				result = "";
			}
			return result;
		}
		public static void Extract()
		{
			NetworkInfo networkInfo = null;
			if (NetworkManager._mInformations.Count > 0)
			{
				foreach (NetworkInfo current in NetworkManager._mInformations.Values)
				{
					if (current.Status == NetConnectionStatus.Connected)
					{
						networkInfo = current;
						break;
					}
					networkInfo = current;
				}
			}
			if (networkInfo == null)
			{
				networkInfo = new NetworkInfo
				{
					DeviceName = "",
					MacAddress = "",
					AdapterType = "",
					Ip = "0.0.0.0",
					IpWan = "0.0.0.0",
					Mask = "0.0.0.0",
					DefaultGateway = "0.0.0.0",
					ConnectionID = ""
				};
			}
			networkInfo.IpWan = NetworkManager.GetIpWan();
			NetworkManager._networkInfo = networkInfo;
		}
		private static string GetIpWan()
		{
			string result;
			try
			{
				WebRequest webRequest = WebRequest.Create("http://checkip.dyndns.org/");
				webRequest.Credentials = CredentialCache.DefaultCredentials;
				HttpWebResponse httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
				Console.WriteLine(httpWebResponse.StatusDescription);
				Stream responseStream = httpWebResponse.GetResponseStream();
				StreamReader streamReader = new StreamReader(responseStream);
				string text = streamReader.ReadToEnd();
				int num = text.IndexOf(":");
				text = text.Remove(0, num + 1);
				int startIndex = text.LastIndexOf("</body>");
				text = text.Remove(startIndex, 14);
				streamReader.Close();
				responseStream.Close();
				httpWebResponse.Close();
				result = text.Trim();
			}
			catch (Exception)
			{
				result = "0.0.0.0";
			}
			return result;
		}
		private static void SetIp(NetworkInfo info)
		{
			ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
			using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementClass.GetInstances().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ManagementObject managementObject = (ManagementObject)enumerator.Current;
					try
					{
						if (info.Status != NetConnectionStatus.Connected)
						{
							info.Ip = "0.0.0.0";
							info.Mask = "0.0.0.0";
							info.DefaultGateway = "0.0.0.0";
						}
						if ((bool)managementObject["ipEnabled"])
						{
							if (managementObject["MACAddress"].ToString().Equals(info.MacAddress))
							{
								string[] array = (string[])managementObject["IPAddress"];
								info.Ip = array[0];
								string[] array2 = (string[])managementObject["IPSubnet"];
								info.Mask = array2[0];
								string[] array3 = (string[])managementObject["DefaultIPGateway"];
								info.DefaultGateway = array3[0];
								break;
							}
						}
					}
					catch (Exception ex)
					{
						Debug.WriteLine("[SetIP]:" + ex.Message);
					}
				}
			}
		}
	}
}
